/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : mf_config.c
  * @brief          : MCU FUNCTION CONFIG
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 FMSH.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by FMSH under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "mf_config.h"
#include "main.h"

/* Private function prototypes -----------------------------------------------*/
void vUartInit(void);
void vGPIOInit(void);
void vPowerKeyInit(void);
void vLPTIM32TimerBaseInit(void);

/**
  * @brief  The application entry point.
  * @retval int
  */
void MF_Clock_Init(void)
{
    FL_CMU_XTHF_WriteDriverStrength(0x0A);
    FL_CMU_XTHF_Enable();
    FL_DelayMs(2);
    /* Initial RCHF */
    FL_CMU_RCHF_SetFrequency(FL_CMU_RCHF_FREQUENCY_8MHZ);
    FL_CMU_RCHF_Enable();

    /* Initial System Clock */
    FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_0CYCLE);
    FL_CMU_SetSystemClockSource(FL_CMU_SYSTEM_CLK_SOURCE_XTHF);
    FL_CMU_SetAHBPrescaler(FL_CMU_AHBCLK_PSC_DIV1);
    FL_CMU_SetAPBPrescaler(FL_CMU_APBCLK_PSC_DIV1);

    SystemCoreClockUpdate();                                                     /*系统时钟更新*/
}

void MF_Config_Init(void)
{
	vGPIOInit();
	vPowerKeyInit();
	vLPTIM32TimerBaseInit();
	vUartInit();
	
	// power on
	POWER_ON();
	
	FL_LPTIM32_Enable(LPTIM32);
	
	SET_ENTER_APP_TIMER();
}

void vUartInit(void)
{
	FL_GPIO_InitTypeDef    GPIO_InitStruct;
	FL_UART_InitTypeDef    UART0_InitStruct;
	
    GPIO_InitStruct.pin           = FL_GPIO_PIN_2;
    GPIO_InitStruct.mode          = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType    = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull          = FL_ENABLE;
    GPIO_InitStruct.remapPin      = FL_DISABLE;
    GPIO_InitStruct.analogSwitch  = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.pin           = FL_GPIO_PIN_3;
    GPIO_InitStruct.mode          = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType    = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull          = FL_DISABLE;
    GPIO_InitStruct.remapPin      = FL_DISABLE;
    GPIO_InitStruct.analogSwitch  = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	
    UART0_InitStruct.clockSrc          = FL_CMU_UART0_CLK_SOURCE_APBCLK;
    UART0_InitStruct.baudRate          = 115200;
    UART0_InitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;
    UART0_InitStruct.dataWidth         = FL_UART_DATA_WIDTH_8B;
    UART0_InitStruct.stopBits          = FL_UART_STOP_BIT_WIDTH_1B;
    UART0_InitStruct.parity            = FL_UART_PARITY_NONE;

    FL_UART_Init(UART0, &UART0_InitStruct);
	
	FL_UART_ClearFlag_RXBuffFull(UART0);
    FL_UART_EnableIT_RXBuffFull(UART0);
    FL_UART_ClearFlag_TXShiftBuffEmpty(UART0);
    //FL_UART_EnableIT_TXShiftBuffEmpty(UART0);
	
	FL_NVIC_ConfigTypeDef    InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 0x01;
    FL_NVIC_Init(&InterruptConfigStruct, UART0_IRQn);
}

void vGPIOInit(void)
{
	/* inidator LED */
    FL_GPIO_InitTypeDef GPIO_InitStruct;

    GPIO_InitStruct.pin             = FL_GPIO_PIN_8;
    GPIO_InitStruct.mode            = FL_GPIO_MODE_OUTPUT;
    GPIO_InitStruct.outputType      = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull            = FL_DISABLE;
    GPIO_InitStruct.remapPin        = FL_DISABLE;
    GPIO_InitStruct.analogSwitch    = FL_DISABLE;
    FL_GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	/* power hold */
    GPIO_InitStruct.pin             = FL_GPIO_PIN_4;
    GPIO_InitStruct.mode            = FL_GPIO_MODE_OUTPUT;
    GPIO_InitStruct.outputType      = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull            = FL_DISABLE;
    GPIO_InitStruct.remapPin        = FL_DISABLE;
    GPIO_InitStruct.analogSwitch    = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void vPowerKeyInit(void)
{
	FL_GPIO_InitTypeDef GPIO_InitStruct;
	
	/* power key */
	GPIO_InitStruct.pin          = FL_GPIO_PIN_5;
    GPIO_InitStruct.mode         = FL_GPIO_MODE_INPUT;
    GPIO_InitStruct.outputType   = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull         = FL_DISABLE;
    GPIO_InitStruct.remapPin     = FL_DISABLE;
    GPIO_InitStruct.analogSwitch = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	FL_EXTI_CommonInitTypeDef    EXTI_InitStruct;
	FL_EXTI_InitTypeDef    EXTIInitStruct;
	
	EXTI_InitStruct.clockSource = FL_CMU_EXTI_CLK_SOURCE_HCLK;
	FL_EXTI_CommonInit(&EXTI_InitStruct);
	
	EXTIInitStruct.filter      = FL_ENABLE;
    EXTIInitStruct.input       = FL_GPIO_EXTI_INPUT_GROUP1;
    EXTIInitStruct.triggerEdge = FL_GPIO_EXTI_TRIGGER_EDGE_FALLING;

    FL_EXTI_Init(FL_GPIO_EXTI_LINE_1, &EXTIInitStruct);
	
	// NVIC
	FL_NVIC_ConfigTypeDef    InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 0x02;
    FL_NVIC_Init(&InterruptConfigStruct, GPIO_IRQn);
}

void vLPTIM32TimerBaseInit(void)
{
	FL_NVIC_ConfigTypeDef InterruptConfigStruct;
	FL_LPTIM32_InitTypeDef TimerBase_InitStruct;

    TimerBase_InitStruct.clockSource = FL_CMU_LPTIM32_CLK_SOURCE_APBCLK;
    TimerBase_InitStruct.prescalerClockSource = FL_LPTIM32_CLK_SOURCE_INTERNAL;
    TimerBase_InitStruct.prescaler = FL_LPTIM32_PSC_DIV8;
    TimerBase_InitStruct.autoReload = 50000; // 50ms
    TimerBase_InitStruct.mode = FL_LPTIM32_OPERATION_MODE_NORMAL;
    TimerBase_InitStruct.onePulseMode = FL_LPTIM32_ONE_PULSE_MODE_CONTINUOUS;
    TimerBase_InitStruct.triggerEdge = FL_LPTIM32_ETR_TRIGGER_EDGE_RISING;
    TimerBase_InitStruct.countEdge = FL_LPTIM32_ETR_COUNT_EDGE_RISING;

    FL_LPTIM32_Init(LPTIM32, &TimerBase_InitStruct);
	
	FL_LPTIM32_ClearFlag_Update(LPTIM32);
    FL_LPTIM32_EnableIT_Update(LPTIM32);

    InterruptConfigStruct.preemptPriority = 0x01;
    FL_NVIC_Init(&InterruptConfigStruct, LPTIMx_IRQn);
}

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */

    /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN Assert_Failed */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END Assert_Failed */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT FMSH *****END OF FILE****/
